﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Form2 console = new Form2();
        Form3 settings = new Form3();
        StreamReader outputStream = StreamReader.Null;
        string output = "";
        Process newProcess;
        bool AppRuns = false;
        Thread thread2;
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd,
                         int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        public Form1()
        {
            InitializeComponent();
            timer1.Start();
            thread2 = new Thread(new ThreadStart(thread2_progression));
            thread2.Name = "test";
            thread2.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (AppRuns == true)
            {
                newProcess.Kill();
                AppRuns = false;
            }
            else
            {
                if (settings.get_setting() != "")
                {
                    newProcess = new Process();
                    newProcess.StartInfo.FileName = settings.get_setting();
                    newProcess.StartInfo.Arguments = "-a " + Properties.Settings.Default.ClientIp + " -s " + Properties.Settings.Default.ClientSubnetMask + " -c " + Properties.Settings.Default.NetworkName + " -k " + Properties.Settings.Default.EncryptionCode + " -l " + Properties.Settings.Default.ServerIp + ":" + Properties.Settings.Default.ServerPort;
                    newProcess.StartInfo.UseShellExecute = false;
                    newProcess.StartInfo.CreateNoWindow = true;
                    newProcess.StartInfo.RedirectStandardOutput = true;
                    newProcess.Exited += new EventHandler(newProcess_Exited);
                    newProcess.Start();
                    AppRuns = true;
                }
                else
                {
                    MessageBox.Show("Du skal indstille hvor din edge.exe er!");
                }
            }
        }

        private void newProcess_Exited(object sender, System.EventArgs e)
        {
            AppRuns = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            console.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            console.ChangeTextBox("Allan");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (AppRuns == true)
            {
                button1.Text = "Stop";
                button1.ForeColor = Color.Red;
                label1.Text = "Your ip is: " + Properties.Settings.Default.ClientIp;
            }
            else
            {
                button1.Text = "Start";
                button1.ForeColor = Color.LightGreen;
                label1.Text = "You are not connected";
            }
        }

        private void thread2_progression()
        {
            while (true)
            {
                if (AppRuns == true)
                {
                    outputStream = newProcess.StandardOutput;
                    output = outputStream.ReadLine();
                    if (output != null)
                    {
                        console.ChangeTextBox(output);
                    }
                }
            }
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            settings.Show();
        }

        private void rectangleShape2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void rectangleShape3_Click(object sender, EventArgs e)
        {

        }

        private void rectangleShape2_Click_1(object sender, EventArgs e)
        {
            timer1.Stop();
            thread2.Abort();
            Close();
        }

        private void rectangleShape3_Click_1(object sender, EventArgs e)
        {

        }

        private void button1_MouseHover(object sender, EventArgs e)
        {
            // button1.ForeColor = Color.LightGreen;
        }

        private void button1_MouseLeave(object sender, EventArgs e)
        {
            // button1.ForeColor = Color.White;
        }

        private void button2_MouseHover(object sender, EventArgs e)
        {
            button2.ForeColor = Color.LightGreen;
        }

        private void button2_MouseLeave(object sender, EventArgs e)
        {
            button2.ForeColor = Color.White;
        }

        private void button3_MouseHover(object sender, EventArgs e)
        {
            button3.ForeColor = Color.LightGreen;
        }

        private void button3_MouseLeave(object sender, EventArgs e)
        {
            button3.ForeColor = Color.White;
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }
    }
}
